<?php
$file = "fifo_x";
if (!posix_access($file,POSIX_F_OK)){
    if (posix_mkfifo($file,0666)){
        fprintf(STDOUT,"create ok\n");
    }
}

$pid = pcntl_fork();
if ($pid==0){
   $fd = fopen($file,"r+");
   stream_set_blocking($fd,0);
   $i=0;
   while (1){
      $data = fread($fd,5);
      echo $i++;
      echo "\r\n";
      if ($data){
          fprintf(STDOUT,"read process pid=%d,recv:%s\n",getmypid(),$data);
          break;
      }
   }
   exit(0);
}
$fd = fopen($file,"w+");
stream_set_blocking($fd,0);
$len = fwrite($fd,"hello",5);
fprintf(STDOUT,"write process pid=%d,write len=%d\n",posix_getpid($data));
fclose($fd);
$pid = pcntl_wait($status);
if ($pid>0){
    fprintf(STDOUT,"exit pid=%d\n",$pid);
}
